package com.qianfeng.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.dto.Page;
import com.qianfeng.entity.Department;
import com.qianfeng.entity.Employee;
import com.qianfeng.entity.Role;
import com.qianfeng.mapper.EmployeeMapper;
import com.qianfeng.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Transactional
    @Override
    public void addEmployee(Employee employee) {
        employee.setDepartment(new Department(employee.getDeptId()));
        employee.setRole(new Role(employee.getRoleId()));
        employeeMapper.addEmployee(employee);

    }

    @Transactional
    @Override
    public void deleteEmployee(Integer empId) {
        employeeMapper.deleteEmployee(empId);
    }

    @Transactional
    @Override
    public void updateEmployee(Employee employee) {
        employee.setDepartment(new Department(employee.getDeptId()));
        employee.setRole(new Role(employee.getRoleId()));
        employeeMapper.updateEmployee(employee);
    }

    @Transactional(readOnly = true)
    @Override
    public Employee getEmployee(Integer empId) {
        return employeeMapper.getEmployee(empId);
    }

    @Transactional(readOnly = true)
    @Override
    public Page getEmployeePage(Integer pageNo, Integer pageSize) {
        Page page = new Page();
        PageHelper.startPage(pageNo, pageSize);
        List<Employee> employeeList = employeeMapper.getEmployeeList();
//        System.out.println("==========="+employeeList);
        PageInfo<Employee> info = new PageInfo<>(employeeList);
        page.setHasNext(info.isHasNextPage());
        page.setHasPre(info.isHasPreviousPage());
        page.setPageCount(info.getPages());
        page.setPageNo(info.getPageNum());
        page.setPageSize(info.getPageSize());
        page.setPage(info.getList());
        return page;
    }

    @Transactional(readOnly = true)
    @Override
    public Employee getEmployeeByEmail(String email) {
        return employeeMapper.getEmployeeByEmail(email);
    }


}
